<?php
class RequireLogin extends CBehavior
{
	public function attach($owner)
	{
		$owner->attachEventHandler('onBeginRequest', array($this, 'handleBeginRequest'));
	}
	
	public function handleBeginRequest($event)
	{
		
		// Create list of allowed urls.
		// Those urls should be accessed during upgrade process too.
		foreach (Yii::app()->params['allowedGuestUserRoutes'] as $allowedGuestUserRoute)
		{
			$allowedGuestUserUrls[] = Yii::app()->createUrl($allowedGuestUserRoute);
		}
		$requestedUrl = Yii::app()->getRequest()->getUrl();
		$isUrlAllowedToGuests = false;
		foreach ($allowedGuestUserUrls as $url)
		{
			if (strpos($requestedUrl, $url) === 0)
			{
				$isUrlAllowedToGuests = true;
			}
		}

		if (Yii::app()->user->isGuest) {
			if (!$isUrlAllowedToGuests)
			{
				Yii::app()->user->loginRequired();
			}
// 			if(isset($_GET['r']) && !$isUrlAllowedToGuests)
// 				Yii::app()->user->loginRequired();
// 			else if(!isset($_GET['r']))
// 				Yii::app()->user->loginRequired();
				
		}
	}
}
?>